﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
namespace LightSwitchApplication
{
    public partial class EditableSalesOrders
    {
        partial void gridAddAndEditNew_CanExecute(ref bool result)
        {
            // Write your code here.

        }

        partial void gridAddAndEditNew_Execute()
        {
            this.Application.ShowCreateNewSalesOrder(0);
        }

        partial void ImportFromExcel_Execute()
        {
            ExcelImporter.Importer.ImportFromExcel(OrdersByDate);
        }


        partial void gridEditSelected_Execute()
        {
            if (OrdersByDate.SelectedItem != null) 
                this.Application.ShowSalesOrderDetail(OrdersByDate.SelectedItem.Id);
        }

        partial void EditableSalesOrders_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            string userName = this.Application.User.FullName;

            if (this.Application.User.HasPermission(Permissions.CanEditSalesOrder))//admin
            {
            }
            else if (this.Application.User.HasPermission(Permissions.CanManageMySalesOrder))//sales person
            {
                //for some reason LINQ does not work with external datasource link
                //var currentSalesPerson = this.DataWorkspace.ApplicationData.SalesPersons.Where(s => s.aspnet_Profile.PropertyValuesString == userName).FirstOrDefault();
                //if (currentSalesPerson != null)
                //{
                //    this.SalesPersonId = currentSalesPerson.Id;
                //    this.VisitProperty.SalesPerson = currentSalesPerson;
                //    this.FindControl("SalesPerson").IsReadOnly = true;
                //}
                this.SalesPersonId = -1;
                this.CustomerId = -1;
                //sales persons
                foreach (SalesPerson item in this.DataWorkspace.ApplicationData.SalesPersons)
                {
                    if (item.aspnet_Profile != null && item.aspnet_Profile.PropertyValuesString == userName)
                    {
                        this.SalesPersonId = item.Id;
                        break;
                    }
                }
                //customers
                foreach (Customer item in this.DataWorkspace.ApplicationData.Customers.Where(c => c.aspnet_Profile_UserId != null))
                {
                    if (item.aspnet_Profile != null && item.aspnet_Profile.PropertyValuesString == userName)
                    {
                        this.CustomerId = item.Id;
                        this.FindControl("SalesPerson").IsReadOnly = true;
                        break;
                    }
                }
            }
            else
            {
                this.SalesPersonId = -1;
                this.CustomerId = -1;
            }
        }

    }
}
